<?php
class UserAction extends Action {

	function _initialize(){
		$this->setTitle( L('user_base_title') );
	}
	
	function index() {
		//个人信息
		$space                     = D('Space')->get_Space($this->mid);
		$space['visitor']          = D('Visitor','space')->where('uid='.$this->mid)->limit(12)->order('cTime DESC')->findall();
		$space['info']             = D('Space')->where('uid='.$this->uid)->field('credit_exp,credit_score,cardId')->find();
		$space['walls']            = D('Wall','space')->getWall($this->mid,8);
		$space['record']           = D('Record')->getNewData($this->mid);
		$space['notify']['list']   = $notify = X('Notify')->get("isread=0 AND touid=".$this->mid);
		$space['notify']['count']  = M('Notify')->where("isread=0 AND touid=".$this->mid)->count();
		$space['followMe'] = $this->_followMe();
		$space['followYou'] = $this->_followYou();
		$space['grouplist']        = D('Group','group')->getUserJoinList($this->mid);

		//转换url [杨德升添加]
		$space['record']		   = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $space['record']);
		
		$this->assign('space',$space);

		//关注统计
		$dao = service('Follow');
		$this->assign('FollowedCount', $dao->getDataCount($this->mid,'user'));
		$this->assign('FollowCount', $dao->getUserCount($this->mid,'user'));
		$this->display();
	}

	//ajax加载动态
	function feed(){
		$strType = ($_POST['type'])?$_POST['type']:'all';
		$intFid  = intval( $_POST['fid'] );
		if($strType=='attention'){
			$map['uid'] = array('in',x('Follow')->getFollowData($this->mid,'user'));
			$map['display'] = 0;
		}elseif ($strType=='group'){
			$group = D('Group','group')->getUserJoinList($this->mid);
			$map['appname'] = 'group';
			if( $intFid && in_array( $intFid,$group['gids'] ) ){
				$map['fid']     = $intFid;
			}else{
				$map['fid']     = array('IN',$group['gids']);
			}
			$space['fid'] = $intFid;
		}else{
			$map['display'] = 0;
		}
		
		//$map['type'] = 'space_record';
		$space['feeds']            = X('Feed')->get($map,10,true);
		$this->assign('space',$space);
		$this->assign('getall',intval($_POST['getall']));
		$this->assign('type',$strType);
		$this->display();
	}
	
	//忽略动态
	function ignoreFeed(){
		$intId = intval(intval($_POST['id']),$this->mid);
		echo x('Feed')->ignore($intId);
	}

	//添加评论
	function docomment(){
		$content = htmlspecialchars( $_POST['content'] , ENT_QUOTES );
		$type = explode('_',$_POST['fid']);
		$data['type']    = trim($type[0]);
		$data['appid']   = intval($type[1]);
		$data['uid']     = $this->mid;
		$data['comment'] = $content;
		$data['toId']    = intval($_POST['replyuid']);
		$data['cTime']   = time();
		$id = M('comment')->add($data);
		if($id){
			$data['id'] = $id;
			$data['uname'] = getUserName($data['uid']);
			$data['uhead'] = getUserFace($data['uid'],'s');
			
			if($data['toId']){
				$info = D('Record')->where('id='.$data['appid'])->field('id,content,uid')->find();
				$title['type'] = '<a href='.U('space/View/record',array('uid'=>$info['uid'],'rid'=>$info['id'])).'>记录</a>';
				$body['comment'] = $content;
				//x('Notify')->put('home_commentcomment',$data['toId'],$title,$body);
			}else{
				$info = D('Record')->where('id='.$type[1])->field('id,content,uid')->find();
				$title['content']  = $info['content'];
				$title['id']  = $info['id'];
				$title['uid']  = $info['uid'];
				$body['comment'] = $content;
				x('Notify')->put('home_recordcomment',$info['uid'],$title,$body);
			}

			//转换url [杨德升添加]
			$data['comment'] = preg_replace('/((?:https?|mailto).*?)(\s|　|&nbsp;|<br|\'|\"|$)/', '<a href="\1" target="_blank">\1</a>\2', $data['comment']);
			
			$this->success($data);
		}else{
			$this->error( L('add_error') );
		}
	}
	
	//删除评论
	function dodelcomment(){
		$result = M('comment')->where("id=".intval($_POST['id']))->delete();
		if($result){
			$this->success( L('del_success') );
		}else{
			$this->error( L('del_error') );
		}
	}
	

	public function dosearch(){
		$grouplist = M('user_colleague_group')->where("uid={$this->mid} AND name LIKE '".$_REQUEST['q']."%'")->field('id,name')->findall();
		if( $grouplist ){
			foreach($grouplist as $key=>$value){
				$friendlist =  $list = M('')->table(C('DB_PREFIX')."user_colleague a LEFT JOIN ".C('DB_PREFIX')."user_colleague_relation b ON a.cid=b.cid")->where("a.uid={$this->mid} AND b.gid=".$value['id'])->field('a.fid')->findall() ;
				$friendlist = array_unique( getSubByKey( $friendlist , 'fid' ) );
				$grouplist[$key]['short']      = '_g'.$value['id'];
				$grouplist[$key]['cn']         = $value['name'];
				$grouplist[$key]['department'] = count($friendlist).'人';
				$grouplist[$key]['uid']        = implode( ',' , $friendlist );
				$grouplist[$key]['type']       = 'group';
			}
		}

		/** Ldap 查询关闭 
		$server = X('DomainAuth');
		$list = $server->getFuzzyMatching($_REQUEST['q'],$_REQUEST['limit']);
		$short = getSubByKey($list,'short');
		$map['uname'] = array('in',$short);
		$uidList = M('User')->where($map)->field("uid,uname as short,fullname,'webuser' as type")->findAll();
	    **/
		$uidList = M('User')->where("uname LIKE '".$_REQUEST['q']."%'")->field("uid,uname as short,uname as sn,fullname,'webuser' as type")->findAll();
		$expert = model('UserGroup')->getGroupUsers(190,true);
		$uid = arrayJoin($uidList,$expert,'uid');
		//$list = arrayJoin($list,$uid,'short');
		$list = $uid;
		if(isset($_REQUEST['reg']) && $_REQUEST['reg'] == 1){
			$result = array();
			foreach($list as $key=>$value){
				if(isset($value['uid'])){
					$list[$key]['cn']   = $list[$key]['fullname'];
					$result[] = $list[$key];
				}else{
					unset( $list[$key] );
				}
			}
			///exit(json_encode($result));
		}
		
		foreach($list as $key=>$value){
			if( $value['type']!='webuser' ){
				 $list[$key]['type']='domain';
			}
			$list[$key]['department'] = ($list[$key]['department'])?$list[$key]['department']:L('no_department');
		}
		$list = sortByCol($list,'uid',SORT_DESC);
		
		if($grouplist){
			$list = array_merge( $grouplist , $list );
		}
		

		exit(json_encode($list));
	}

	
	protected function getUserId($uname){
		$info = M('User')->where("uname='$uname'")->field('uid')->find();
		if($info){
			return $info['uid'];
		}
	}
	
	function userinfo(){
		if($_GET['uid']){
			$info = D('Space')->get_Base_Data($this->uid);
			$this->assign('info',$info);
			$this->display();
		}else{
			echo 'No Uid';
		}
	}
	
	//我关注的人
	function _followYou(){
		$dao = service('Follow');
		
		$map['uid'] = $this->mid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'aimId',0);
		foreach($list as $k=>&$v){
			if($k>8) unset($list[$k]);
			$v = $v['aimId'];
		}
		
		return $list;
	}
	
	//关注我的人
	function _followMe(){
		$dao = service('Follow');
		
		$map['aimId'] = $this->mid;
		$map['type'] = 'user';
		$list = $dao->getFollow($map,'uid',0);
		foreach($list as $k=>&$v){
			if($k>8) unset($list[$k]);
			$v = $v['uid'];
		}	
			
		return $list;		
	}	
}
?>